################### Data was pulled from the 1996-2012 New York Times Presidential Election Exit Polls
# Source for 1996-2008: http://elections.nytimes.com/2008/results/president/national-exit-polls.html
# Source for 2012: http://elections.nytimes.com/2012/results/president/exit-polls

repincome<-read.csv("figureA2_a.csv")
plot(repincome$year96,type="o",pch=21,lty=2, xaxt="n",xlim=c(1,6),ylim=c(20,60),col="grey85",xlab="Household Income",ylab="Republican Candidate Vote Share, Presidential Elections")
axis(1, at=1:6, lab=c("<$15K","15-30K","$30-50K","$50-75K","$75-100K",">$100K"))
lines(repincome$year00,col="grey65",type="o",pch=22, lty=3)
lines(repincome$year04,col="grey45",type="o",pch=23,lty=4)
lines(repincome$year08,col="grey25",type="o",pch=24,lty=5)
lines(repincome$year12,col="black",type="o",pch=25,lty=6)
legend(1,60, c("1996","2000", "2004", "2008", "2012"), cex=0.8, 
   col=c("grey85","grey65","grey45","grey25","black"), pch=21:25, lty=2:6)

################### Data was pulled from the U.S. Census (DeNavas-Walt, Richardson, and Stringfellow 2010)
# Source: https://www.census.gov/prod/2010pubs/p60-238.pdf (p.5-6)

error.bar <- function(x, y, upper, lower=upper, length=0.1,...){
if(length(x) != length(y) | length(y) !=length(lower) | length(lower) != length(upper))
stop("vectors must be same length")
arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, ...)
}

rawdata1 <- matrix(c(28034, 30941, 30653, 18135, 15063, 51861, 54461, 65469, 32584, 38039),5,2)
ee1 <- matrix(c(183, 214, 1053, 327, 276, 253, 459, 2084, 648, 826),5,2)

yrange <- c(10000, 70000)
ticks <- pretty(yrange)
labels <- format(ticks, big.mark=",", scientific=FALSE)

barx1 <- barplot(rawdata1, beside=T, names.arg=c("Median Per Capita Income", "Median Household Income"), ylim=c(yrange), xpd=F, col=c("gray85", "gray65", "gray45", "gray25", "gray5"),cex.names=.85, axis.lty=1,ylab="Income (2009 USD)", font.lab=2, cex=1.1, cex.main=1.2, cex.lab=1.2, axes = FALSE) 
axis(2, at = ticks, labels = labels, cex.axis=1.1) 
legend(1,70000,pch=c(15,15,15), col=c("gray85", "gray65", "gray45", "gray25", "gray5"),c("White", "White, Not Hispanic", "Asian", "Black", "Hispanic"), cex=1.1)
error.bar(barx1,rawdata1,ee1)
